/**
 * Three.js 辅助函数库
 * 提供3D场景常用的工具方法
 */

import * as THREE from 'three'

export class ThreeHelper {
  /**
   * 创建基础场景配置
   * @param {HTMLElement} container 容器元素
   * @returns {Object} 包含scene, camera, renderer的对象
   */
  static createBasicScene(container) {
    // 场景
    const scene = new THREE.Scene()
    scene.background = new THREE.Color(0x1a1a2e)
    
    // 相机
    const camera = new THREE.PerspectiveCamera(
      75,
      container.clientWidth / container.clientHeight,
      0.1,
      1000
    )
    camera.position.set(0, 5, 10)
    
    // 渲染器
    const renderer = new THREE.WebGLRenderer({ antialias: true })
    renderer.setSize(container.clientWidth, container.clientHeight)
    renderer.shadowMap.enabled = true
    renderer.shadowMap.type = THREE.PCFSoftShadowMap
    
    container.appendChild(renderer.domElement)
    
    return { scene, camera, renderer }
  }
  
  /**
   * 创建基础灯光配置
   * @param {THREE.Scene} scene 场景对象
   */
  static setupBasicLights(scene) {
    // 环境光
    const ambientLight = new THREE.AmbientLight(0x404040, 0.6)
    scene.add(ambientLight)
    
    // 主光源
    const directionalLight = new THREE.DirectionalLight(0xffffff, 1)
    directionalLight.position.set(10, 10, 5)
    directionalLight.castShadow = true
    directionalLight.shadow.mapSize.width = 2048
    directionalLight.shadow.mapSize.height = 2048
    scene.add(directionalLight)
    
    return { ambientLight, directionalLight }
  }
  
  /**
   * 窗口大小调整处理
   * @param {THREE.Camera} camera 相机对象
   * @param {THREE.WebGLRenderer} renderer 渲染器对象
   * @param {HTMLElement} container 容器元素
   */
  static handleWindowResize(camera, renderer, container) {
    camera.aspect = container.clientWidth / container.clientHeight
    camera.updateProjectionMatrix()
    renderer.setSize(container.clientWidth, container.clientHeight)
  }
  
  /**
   * 创建简单几何体
   * @param {string} type 几何体类型
   * @param {Object} options 配置参数
   * @returns {THREE.Mesh} 网格对象
   */
  static createSimpleGeometry(type, options = {}) {
    let geometry
    const {
      width = 1,
      height = 1,
      depth = 1,
      radius = 1,
      color = 0x00ff88
    } = options
    
    switch (type) {
      case 'box':
        geometry = new THREE.BoxGeometry(width, height, depth)
        break
      case 'sphere':
        geometry = new THREE.SphereGeometry(radius, 32, 32)
        break
      case 'cylinder':
        geometry = new THREE.CylinderGeometry(radius, radius, height, 32)
        break
      default:
        geometry = new THREE.BoxGeometry(1, 1, 1)
    }
    
    const material = new THREE.MeshPhongMaterial({ color })
    const mesh = new THREE.Mesh(geometry, material)
    mesh.castShadow = true
    mesh.receiveShadow = true
    
    return mesh
  }
  
  /**
   * 创建粒子系统
   * @param {THREE.Scene} scene 场景对象
   * @param {Object} options 粒子配置
   */
  static createParticleSystem(scene, options = {}) {
    const {
      count = 1000,
      color = 0x00ff88,
      size = 0.1,
      range = 50
    } = options
    
    const particles = new THREE.BufferGeometry()
    const positions = new Float32Array(count * 3)
    
    for (let i = 0; i < count * 3; i += 3) {
      positions[i] = (Math.random() - 0.5) * range
      positions[i + 1] = (Math.random() - 0.5) * range
      positions[i + 2] = (Math.random() - 0.5) * range
    }
    
    particles.setAttribute('position', new THREE.BufferAttribute(positions, 3))
    
    const material = new THREE.PointsMaterial({ color, size })
    const particleSystem = new THREE.Points(particles, material)
    
    scene.add(particleSystem)
    return particleSystem
  }
}

// 常用颜色配置
export const COLORS = {
  PRIMARY: 0x00ff88,
  SECONDARY: 0x1890ff,
  SUCCESS: 0x52c41a,
  WARNING: 0xfa8c16,
  ERROR: 0xff4d4f,
  GOLD: 0xFFD700,
  SILVER: 0xC0C0C0,
  BACKGROUND: 0x1a1a2e
}

// 常用材质配置
export const MATERIALS = {
  BASIC: THREE.MeshBasicMaterial,
  PHONG: THREE.MeshPhongMaterial,
  LAMBERT: THREE.MeshLambertMaterial,
  STANDARD: THREE.MeshStandardMaterial
}

console.log('ThreeHelper 工具库已加载 - 等待开发完善') 